package com.qiaoba.module.system.service.impl;

import com.qiaoba.api.auth.utils.SecurityUtil;
import com.qiaoba.api.file.entity.vo.FileUploadVO;
import com.qiaoba.api.file.service.FileApiService;
import com.qiaoba.api.system.entity.dto.UserBasicInfoDTO;
import com.qiaoba.common.base.context.UserInfoContext;
import com.qiaoba.common.base.exception.ServiceException;
import com.qiaoba.module.system.entity.SysUser;
import com.qiaoba.module.system.service.SysProfileService;
import com.qiaoba.module.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 个人信息 服务层实现
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/19 11:29
 */
@Service
@RequiredArgsConstructor
public class SysProfileServiceImpl implements SysProfileService {

    private final FileApiService fileService;
    private final SysUserService sysUserService;

    @Override
    public void updatePassword(String oldPassword, String newPassword) {
        String userId = UserInfoContext.getUserId();
        String password = sysUserService.selectById(userId, true).getPassword();
        if (!SecurityUtil.matchesPassword(oldPassword, password)) {
            throw new ServiceException("旧密码错误, 请重新输入");
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setPassword(SecurityUtil.encryptPassword(newPassword));
        sysUserService.updateById(sysUser);
    }

    @Override
    public void updateBasicInfo(UserBasicInfoDTO dto) {
        SysUser sysUser = new SysUser();
        sysUser.setUserId(UserInfoContext.getUserId());
        sysUser.setNickname(dto.getNickname());
        sysUser.setPhone(dto.getPhone());
        sysUser.setEmail(dto.getEmail());
        sysUser.setGender(dto.getGender());
        sysUser.setAvatar(dto.getAvatar());
        sysUserService.updateById(sysUser);
    }
}
